package handlers

import (
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/gin-tonic/global"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/loopfz/gadgeto/tonic"
)

type MyInput struct {
	Foo int32  `path:"foo" validate:"required,gt=10"`
	Bar string `query:"bar" default:"foobar" validate:"nefield=Baz"`
	Baz string `json:"baz" validate:"required,email"`
}
type MyInput2 struct {
	Foo int32  `path:"foo"`
	Bar string `query:"bar"`
	Baz string `json:"baz"`
}

func addMyinput(root *gin.Engine) {
	g := root.Group("/myinput")
	g.POST("/add/:foo", tonic.Handler(func(c *gin.Context, in *MyInput) error {
		fmt.Printf("--------in---%+v  \n", in)
		return nil
	}, 200))

	// 手动触发校验方式
	g.POST("/add2/:foo", tonic.Handler(func(c *gin.Context, in *MyInput2) error {
		in2 := MyInput{}

		copier.Copy(&in2, in)
		fmt.Printf("--------in---%+v  \n", in2)
		// 手动触发校验
		err := global.ValidateObj.Struct(in2)

		return err
	}, 200))

}
